home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5402 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Sorting pointers to objects with qsort
  5. Date: 4 Feb 1996 10:58:59 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4f23hj$m06@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: 38.8.60.5
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 04, 1996 03:15:12 in article <Sorting pointers to objects with
  15. qsort>, 'mebust@email.unc.edu (Scott Mebust)' wrote: 
  16.  
  17.  
  18. >I'd appreciate any help on the following problem:  sorting pointers to  
  19. >objects (based on object attributes) using qsort. 
  20. >If replying, please send an e-mail as well.  Thanks. 
  21. >Scott 
  22. >mebust@email.unc.edu 
  23. >// QUESTION:  Why does the following code return the wrong results? 
  24. >// I can't seem to figure this one out.  The program below attempts to 
  25. >// sort an array of pointers to String objects using qsort.  It compiles 
  26. >// and may *seem* to return the correct results but does not actually 
  27. >// sort the pointers based on any valid data. 
  28. > [... portions deleted ...> 
  29. >class String 
  30. >{ 
  31. >public: 
  32. >String(const char* ccp) 
  33. > [.... irrelevant code deleted ....] 
  34. >const char * GetValue(void) const 
  35. >{ return str_; } 
  36. >friend ostream& operator<< (ostream& o, String& s); 
  37. >private: 
  38. >char* str_; 
  39. >}; 
  40. ********* here's your problem ******* 
  41. >int StrCmpFn (const void* VP1, const void* VP2) 
  42. >{ 
  43. >return strcmp(((const String*)VP1)->GetValue(), 
  44. >((const String*)VP2)->GetValue()); 
  45. >} 
  46.  
  47. The comparison function actually gets pointers to the 
  48. elements of the array, which themselves are pointers. 
  49. So, you need another dereference: 
  50.  
  51. int StrCmpFn (const void* VP1, const void* VP2) 
  52.  { 
  53.    const String * s1 = *((String**)(void**)VP1); 
  54.    const String * s2 = *((String**)(void**)VP2); 
  55.    return strcmp(s1->GetValue(), s2->GetValue()); 
  56.  } 
  57.  
  58. Warning!  I didn't actually test this code so I may have a 
  59. subtle error -- it's the idea that counts:-)  VP1 and VP2 
  60. are actually void** so you must first cast them to such. 
  61. Then you cast the void** to String** and dereference 
  62. the result to come up with String*.   
  63.  
  64. You can combine all this into a single line, if you like, 
  65. but as an illustration it would look messy and hard to 
  66. decipher. 
  67.  
  68. BTW, the 'casting' is all done by the compiler.  It does 
  69. not generate any executable code so the cost is totally 
  70. in the amount of typing you have to do (and a millisec 
  71. or two by the compiler trying to figure out what you're 
  72. after). 
  73.  
  74.  
  75. -- 
  76. Pete Grant 
  77. Kalevi, Inc. 
  78. Object Oriented Software Development
  79.